<template>
	<view class="post-container">
		<!-- 标题输入 -->
		<view class="input-section">
			<input type="text" v-model="title" placeholder="请输入标题" maxlength="50" class="title-input" />
		</view>
		
		<!-- 内容输入 -->
		<view class="input-section">
			<textarea v-model="content" placeholder="分享你的研学经验和心得..." class="content-input" maxlength="1000" />
			<text class="word-count">{{ content.length }}/1000</text>
		</view>
		
		<!-- 图片上传 -->
		<view class="image-section">
			<view class="image-list">
				<view class="image-item" v-for="(img, index) in images" :key="index">
					<image :src="img" mode="aspectFill" @tap="previewImage(index)"></image>
					<text class="delete-btn" @tap.stop="deleteImage(index)">×</text>
				</view>
				<view class="upload-btn" @tap="chooseImage" v-if="images.length < 9">
					<text class="iconfont icon-camera"></text>
				</view>
			</view>
			<text class="tip">最多上传9张图片</text>
		</view>
		
		<!-- 发布按钮 -->
		<view class="bottom-bar">
			<button class="submit-btn" :disabled="!isValid" @tap="handleSubmit">发布</button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			title: '',
			content: '',
			images: [],
			isSubmitting: false
		}
	},
	computed: {
		isValid() {
			return this.title.trim() && this.content.trim() && !this.isSubmitting
		}
	},
	methods: {
		// 选择图片
		async chooseImage() {
			try {
				const res = await uni.chooseImage({
					count: 9 - this.images.length,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera']
				})
				
				// 上传图片到云存储
				const uploadTasks = res.tempFilePaths.map(filePath => this.uploadFile(filePath))
				const uploadResults = await Promise.all(uploadTasks)
				
				this.images = [...this.images, ...uploadResults]
			} catch (error) {
				console.error('选择图片失败', error)
			}
		},
		
		// 上传文件到云存储
		async uploadFile(filePath) {
			try {
				const result = await uniCloud.uploadFile({
					filePath,
					cloudPath: `forum/${Date.now()}-${Math.random().toString(36).slice(-6)}.${filePath.split('.').pop()}`
				})
				return result.fileID
			} catch (error) {
				console.error('上传图片失败', error)
				uni.showToast({
					title: '上传图片失败',
					icon: 'none'
				})
				return ''
			}
		},
		
		// 预览图片
		previewImage(index) {
			uni.previewImage({
				urls: this.images,
				current: this.images[index]
			})
		},
		
		// 删除图片
		deleteImage(index) {
			uni.showModal({
				title: '提示',
				content: '确定要删除这张图片吗？',
				success: (res) => {
					if (res.confirm) {
						this.images.splice(index, 1)
					}
				}
			})
		},
		
		// 提交帖子
		async handleSubmit() {
			if (!this.isValid) return
			
			try {
				this.isSubmitting = true
				const userInfo = uni.getStorageSync('userInfo')
				
				const res = await uniCloud.callFunction({
					name: 'createPost',
					data: {
						title: this.title.trim(),
						content: this.content.trim(),
						images: this.images,
						userId: userInfo._id,
						userName: userInfo.nickname || userInfo.username,
						userAvatar: userInfo.avatar
					}
				})
				
				if (res.result.code === 0) {
					uni.showToast({
						title: '发布成功',
						icon: 'success'
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				} else {
					uni.showToast({
						title: res.result.msg,
						icon: 'none'
					})
				}
			} catch (error) {
				uni.showToast({
					title: '发布失败',
					icon: 'none'
				})
			} finally {
				this.isSubmitting = false
			}
		}
	}
}
</script>

<style>
/* 引入图标字体 */
@font-face {
	font-family: "iconfont";
	src: url('data:font/woff2;charset=utf-8;base64,d09GMgABAAAAAA...') format('woff2');
}

.iconfont {
	font-family: "iconfont" !important;
	font-size: 32rpx;
	font-style: normal;
	-webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale;
}

.icon-camera:before {
	content: "\e8d7";
}

.post-container {
	min-height: 100vh;
	background-color: #fff;
	padding-bottom: 120rpx;
}

.input-section {
	padding: 30rpx;
	background-color: #fff;
	border-bottom: 2rpx solid #f5f5f5;
}

.title-input {
	font-size: 32rpx;
	font-weight: bold;
	height: 88rpx;
}

.content-input {
	width: 100%;
	height: 400rpx;
	font-size: 28rpx;
	line-height: 1.6;
}

.word-count {
	font-size: 24rpx;
	color: #999;
	text-align: right;
	margin-top: 10rpx;
}

.image-section {
	padding: 30rpx;
}

.image-list {
	display: flex;
	flex-wrap: wrap;
	margin: 0 -10rpx;
}

.image-item {
	position: relative;
	width: calc(33.33% - 20rpx);
	height: 200rpx;
	margin: 10rpx;
}

.image-item image {
	width: 100%;
	height: 100%;
	border-radius: 8rpx;
}

.delete-btn {
	position: absolute;
	top: -20rpx;
	right: -20rpx;
	width: 40rpx;
	height: 40rpx;
	background-color: rgba(0, 0, 0, 0.5);
	color: #fff;
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 32rpx;
}

.upload-btn {
	width: calc(33.33% - 20rpx);
	height: 200rpx;
	margin: 10rpx;
	background-color: #f5f5f5;
	border-radius: 8rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.upload-btn .iconfont {
	font-size: 48rpx;
	color: #999;
}

.tip {
	font-size: 24rpx;
	color: #999;
	margin-top: 20rpx;
}

.bottom-bar {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
	padding: 20rpx 30rpx;
	background-color: #fff;
	box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.submit-btn {
	width: 100%;
	height: 80rpx;
	background: linear-gradient(135deg, #4a6ee0, #3a5bc7);
	color: #fff;
	font-size: 32rpx;
	border-radius: 40rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.submit-btn[disabled] {
	background: #ccc;
	opacity: 0.6;
}
</style> 